<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="装车时间">
				<el-date-picker v-model="daterangeLoadTime" style="width: 240px" value-format="yyyy-MM-dd"
					type="daterange" range-separator="-" start-placeholder="开始日期"
					end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item label="起点" prop="startPoint">
				<el-input v-model="queryParams.startPoint" placeholder="请输入起点" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="终点" prop="endPoint">
				<el-input v-model="queryParams.endPoint" placeholder="请输入终点" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="货主名" prop="cargoUserId">
				<el-select v-model="queryParams.cargoUserId" placeholder="请选择货主名" clearable>
					<el-option v-for="item in employerList" :key="item.id" :label="item.companyName" :value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="运输类型" prop="traffic">
				<el-select v-model="queryParams.traffic" placeholder="请选择运输类型" clearable>
					<el-option v-for="dict in dict.type.driver_traffic" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="支付方式" prop="payMethod">
				<el-select v-model="queryParams.payMethod" placeholder="请选择支付方式" clearable>
					<el-option v-for="dict in dict.type.employer_cargo_payment" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="货源状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择货源状态" clearable>
					<el-option v-for="dict in dict.type.cargo_source_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['employer:sourcedetails:remove']"
        >删除</el-button>
      </el-col> -->
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['employer:sourcedetails:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="sourcedetailsList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="ID" align="center" prop="id" />
			<el-table-column label="装车时间" align="center" prop="loadTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.loadTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="发货人信息" align="center" prop="consigner.name" />
			<el-table-column label="收货人信息" align="center" prop="consignee.name" />
			<el-table-column label="起点" align="center" prop="startPoint" />
			<el-table-column label="终点" align="center" prop="endPoint" />
			<el-table-column label="货主" align="center" prop="cargoUser.companyName" />
			<el-table-column label="货物信息" align="center" prop="cargoGoods.name" />
			<el-table-column label="用车类型" align="center" prop="carownerVehicletype.vehicleName" />
			<el-table-column label="运输类型" align="center" prop="traffic">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.driver_traffic" :value="scope.row.traffic" />
				</template>
			</el-table-column>
			<el-table-column label="运输费用" align="center" prop="expenses" />
			<el-table-column label="支付方式" align="center" prop="payMethod">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.employer_cargo_payment" :value="scope.row.payMethod" />
				</template>
			</el-table-column>
			<el-table-column label="其他需要，如发票、保险金等" align="center" prop="other" />
			<el-table-column label="备注" align="center" prop="remarks" />
			<el-table-column label="货源状态" align="center" prop="status">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.cargo_source_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="handleUpdate(scope.row)"
						v-hasPermi="['employer:sourcedetails:edit']"
					  >修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['employer:sourcedetails:remove']">删除</el-button>
				</template>
			</el-table-column> -->
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改货源详情对话框 -->
		<!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="装车时间" prop="loadTime">
					<el-date-picker clearable v-model="form.loadTime" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择装车时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="发货人信息" prop="consignerId">
					<el-select v-model="form.consignerId" placeholder="请选择发货人信息" clearable>
						<el-option v-for="item in consignerList" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="收货人信息" prop="consigneeId">
					<el-select v-model="form.consigneeId" placeholder="请选择收货人信息" clearable>
						<el-option v-for="item in consigneeList" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="起点" prop="startPoint">
					<el-input v-model="form.startPoint" placeholder="请输入起点" />
				</el-form-item>
				<el-form-item label="终点" prop="endPoint">
					<el-input v-model="form.endPoint" placeholder="请输入终点" />
				</el-form-item>
				<el-form-item label="货主" prop="cargoUserId">
					<el-select v-model="form.cargoUserId" placeholder="请选择货主" clearable>
						<el-option v-for="item in employerList" :key="item.id" :label="item.companyName"
							:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="货物信息" prop="goodsId">
					<el-select v-model="form.goodsId" placeholder="请选择货物信息" clearable>
						<el-option v-for="item in goodsList" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="用车类型" prop="useCarId">
					<el-select v-model="form.useCarId" placeholder="请选择用车类型" clearable>
						<el-option v-for="item in vehicleTypeList" :key="item.id" :label="item.vehicleName"
							:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="运输类型" prop="traffic">
					<el-select v-model="form.traffic" placeholder="请选择运输类型">
						<el-option v-for="dict in dict.type.driver_traffic" :key="dict.value" :label="dict.label"
							:value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="运输费用" prop="expenses">
					<el-input v-model="form.expenses" placeholder="请输入运输费用" />
				</el-form-item>
				<el-form-item label="支付方式" prop="payMethod">
					<el-select v-model="form.payMethod" placeholder="请选择支付方式">
						<el-option v-for="dict in dict.type.employer_cargo_payment" :key="dict.value"
							:label="dict.label" :value="dict.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="发票" prop="other">
					<el-input v-model="form.other" placeholder="请输入其他需要，如发票、保险金等" />
				</el-form-item>
				<el-form-item label="备注" prop="remarks">
					<el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="逻辑删除标记，0表示未删除，1表示已删除" prop="del">
          <el-input v-model="form.del" placeholder="请输入逻辑删除标记，0表示未删除，1表示已删除" />
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog> -->
	</div>
</template>

<script>
	import {
		listSourcedetails,
		getSourcedetails,
		delSourcedetails,
		addSourcedetails,
		updateSourcedetails
	} from "@/api/employer/sourcedetails";
	import {
		listConsigneeAll
	} from "@/api/employer/consignee";
	import {
		listConsignerAll
	} from "@/api/employer/consigner";
	import {
		listUserAll
	} from "@/api/employer/user";
	import {
		listGoodsAll
	} from "@/api/employer/goods";
	import {
		listVehicletypeAll
	} from "@/api/driver/vehicletype";

	export default {
		name: "Sourcedetails",
		dicts: ['driver_traffic', 'employer_cargo_payment', 'cargo_source_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 货源详情表格数据
				sourcedetailsList: [],
				// 发货人信息表格数据
				consignerList: [],
				// 收货人信息表格数据
				consigneeList: [],
				// 货主信息表格数据
				employerList: [],
				// 货物信息表格数据
				goodsList: [],
				// 用车类型表格数据
				vehicleTypeList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 逻辑删除标记，0表示未删除，1表示已删除时间范围
				daterangeLoadTime: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					loadTime: null,
					consignerId: null,
					consigneeId: null,
					startPoint: null,
					endPoint: null,
					payMethod: null,
					status: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					loadTime: [{
						required: true,
						message: "装车时间不能为空",
						trigger: "blur"
					}],
					consignerId: [{
						required: true,
						message: "发货人信息ID不能为空",
						trigger: "blur"
					}],
					consigneeId: [{
						required: true,
						message: "收货人信息ID不能为空",
						trigger: "blur"
					}],
					startPoint: [{
						required: true,
						message: "起点不能为空",
						trigger: "blur"
					}],
					endPoint: [{
						required: true,
						message: "终点不能为空",
						trigger: "blur"
					}],
					cargoUserId: [{
						required: true,
						message: "货主ID不能为空",
						trigger: "blur"
					}],
					goodsId: [{
						required: true,
						message: "货物信息ID不能为空",
						trigger: "blur"
					}],
					useCarId: [{
						required: true,
						message: "用车类型ID不能为空",
						trigger: "blur"
					}],
					traffic: [{
						required: true,
						message: "运输类型不能为空",
						trigger: "change"
					}],
					expenses: [{
						required: true,
						message: "运输费用不能为空",
						trigger: "blur"
					}],
					payMethod: [{
						required: true,
						message: "支付方式不能为空",
						trigger: "change"
					}],
				}
			};
		},
		created() {
			this.getList();
			this.getListConsignerUser();
			this.getListConsigneeUser();
			this.getListCargoUser();
			this.getListGoods();
			this.getListVehicleType();
		},
		methods: {
			/** 查询货源详情列表 */
			getList() {
				this.loading = true;
				this.queryParams.params = {};
				if (null != this.daterangeLoadTime && '' != this.daterangeLoadTime) {
					this.queryParams.params["beginLoadTime"] = this.daterangeLoadTime[0];
					this.queryParams.params["endLoadTime"] = this.daterangeLoadTime[1];
				}
				listSourcedetails(this.queryParams).then(response => {
					this.sourcedetailsList = response.rows;
					this.total = response.total;
					this.loading = false;
					console.log('----------发货人--------', response.rows)
				});
			},
			/** 查询发货人信息列表 */
			getListConsignerUser() {
				listConsignerAll().then(response => {
					this.consignerList = response.rows;
				});
			},
			/** 查询收货人信息列表 */
			getListConsigneeUser() {
				listConsigneeAll().then(response => {
					this.consigneeList = response.rows;
				});
			},
			/** 查询货主信息列表 */
			getListCargoUser() {
				listUserAll().then(response => {
					this.employerList = response.rows;
				});
			},
			/** 查询货物信息列表 */
			getListGoods() {
				listGoodsAll().then(response => {
					this.goodsList = response.rows;
				});
			},
			/** 查询用车类型列表 */
			getListVehicleType() {
				listVehicletypeAll().then(response => {
					this.vehicleTypeList = response.rows;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					loadTime: null,
					consignerId: null,
					consigneeId: null,
					startPoint: null,
					endPoint: null,
					cargoUser: null,
					goodsId: null,
					useCarId: null,
					traffic: null,
					expenses: null,
					payMethod: null,
					other: null,
					remarks: null,
					status: null,
					del: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.daterangeLoadTime = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加货源详情";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getSourcedetails(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改货源详情";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateSourcedetails(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addSourcedetails(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除货源详情编号为"' + ids + '"的数据项？').then(function() {
					return delSourcedetails(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('employer/sourcedetails/export', {
					...this.queryParams
				}, `sourcedetails_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>